package com.logistics.admin;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.logistics.Pickup;

public class PrintUtlis implements Printable {

	@Override
	public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
		
		Graphics2D g2_1 = (Graphics2D) gra;
		g2_1.setColor(Color.black);
		
		Font font = new Font("宋体", Font.PLAIN, 14);
		Font font2 = new Font("宋体", Font.PLAIN, 12);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		g2_1.setFont(font2); // 设置字体
		// BasicStroke bs_3=new BasicStroke(0.5f);
		float[] dash1 = { 2.0f };
		// 设置打印线的属性。
		// 1.线宽 2、3、不知道，4、空白的宽度，5、虚线的宽度，6、偏移量
		g2_1.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));

		double x = pf.getImageableX();
		double y = pf.getImageableY();
		float heigth1 = font.getSize2D()  - 5; //字体高度
		float row = 2;
		
		g2_1.setFont(font); // 设置字体
		g2_1.drawString(title, (float) x + 30, (float) y + row* heigth1);
		
		row++;
		
		g2_1.drawLine((int) x, (int) (y + row * heigth1), (int) x + 180, (int) (y + row * heigth1));
		row = row + 2;
		g2_1.setFont(font2);
		g2_1.drawString("姓名：" + pickup.getName(), (float) x, (float) y + row * heigth1);
		row = row + 2;
		g2_1.drawString("电话：" + pickup.getPhone(), (float) x, (float) y + row* heigth1);
		row = row + 2; 
		for (int i = 0; i < address.size(); i ++) {
			if (i == 0) {
				g2_1.drawString("地址：" + address.get(i), (float) x, (float) y + row * heigth1);
			} else {
				g2_1.drawString(address.get(i), (float) x, (float) y + row * heigth1);
			}
			
			row = row + 2;
		}
		g2_1.drawString(String.format("重量：%s KG", pickup.getWeight()), (float) x, (float) y + row * heigth1);
		row = row + 2;
		g2_1.setFont(new Font("宋体", Font.PLAIN, 8)); // 设置字体
		if (StringUtils.isNotBlank(pickup.getDescription())) {
			g2_1.drawString("有备注信息，请到系统中查看。", (float) x, (float) y + row * heigth1);
			row = row + 2;
		}
		
		
		g2_1.drawString("时间：" + sdf.format(pickup.getCreateDate()), (int) x, (float) y + row * heigth1);
		row = row + 2;
		
		return 0;
	}

	/**
	 * @param message
	 * @param name
	 */
	public void printMessage(String title, Pickup pickup) {
		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT);
		
		Paper p = new Paper();

		int length = 280;
		p.setSize(200, length);
		p.setImageableArea(5, 50, 227, length);
		pf.setPaper(p);
		book.append(new PrintUtlis(title, pickup), pf);
		PrinterJob job = PrinterJob.getPrinterJob();
		job.setPageable(book);
		try {
			job.print();
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}
	
	public PrintUtlis(String title, Pickup pickup) {
		this.title = title;
		this.pickup = pickup;
		
		List<String> list = new ArrayList<String>();
		int rowLength = 10;
		int subLength = 0;
		String address = pickup.getAddress();
		int length = pickup.getAddress().length() / rowLength;
		for (int i = 0; i <= length ; i++) {
			
			
			if (address.length() <= rowLength) {
				list.add(address.substring(subLength, address.length()));
			} else {
				if (i == 0) {
					list.add(address.substring(subLength, (rowLength - 3)));
					address = address.substring((rowLength - 3), address.length());
				} else {
					list.add(address.substring(subLength, rowLength));
					address = address.substring(rowLength, address.length());
				}
			}
			
		}
		
		this.address = list;
	}
	
	private String title;
	
	public PrintUtlis() {
		
	}
	
	private Pickup pickup;
	
	private List<String> address;
	
	public static void main(String[] args) {
		PrintUtlis printTest = new PrintUtlis();
		
		Pickup pickup = new Pickup();
		pickup.setName("洪仲泽");
		pickup.setPhone("18559691100");
		pickup.setCreateDate(new Date());
		pickup.setAddress("啊撒豆腐干");
		printTest.printMessage("取件信息", pickup);
//		String s = "啊撒豆腐干环境看来请问123";
//		System.out.println(s.substring(0,5));
//		System.out.println((s.length()/10));
	}
}
